using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace CDTM.Modules.TraCuuDiem.Components
{
	public partial class HocSinh
	{
		#region Properties.
		
		public string MaHSSV { set; get; }
		public string TenHocSinh { set; get; }
		public DateTime NgaySinh { set; get; }
		public string DanToc { set; get; }
		public string TonGiao { set; get; }
		public string TPGD { set; get; }
		public bool GioiTinh { set; get; }
		public string HoKhau { set; get; }
		public string CDUuTien { set; get; }
		public string GhiChu { set; get; }
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Select methods.
		
		public static HocSinh Load(string maHSSV)
		{
			const string spName = "[dbo].[p_CDTM_HocSinh_Load]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@MaHSSV", SqlDbType.NVarChar, maHSSV);
			HocSinh entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
			if (reader.Read())
			{
				entity = new HocSinh();
				if (!reader.IsDBNull(reader.GetOrdinal("MaHSSV"))) entity.MaHSSV = reader.GetString(reader.GetOrdinal("MaHSSV"));
				if (!reader.IsDBNull(reader.GetOrdinal("TenHocSinh"))) entity.TenHocSinh = reader.GetString(reader.GetOrdinal("TenHocSinh"));
				if (!reader.IsDBNull(reader.GetOrdinal("NgaySinh"))) entity.NgaySinh = reader.GetDateTime(reader.GetOrdinal("NgaySinh"));
				if (!reader.IsDBNull(reader.GetOrdinal("DanToc"))) entity.DanToc = reader.GetString(reader.GetOrdinal("DanToc"));
				if (!reader.IsDBNull(reader.GetOrdinal("TonGiao"))) entity.TonGiao = reader.GetString(reader.GetOrdinal("TonGiao"));
				if (!reader.IsDBNull(reader.GetOrdinal("TPGD"))) entity.TPGD = reader.GetString(reader.GetOrdinal("TPGD"));
				if (!reader.IsDBNull(reader.GetOrdinal("GioiTinh"))) entity.GioiTinh = reader.GetBoolean(reader.GetOrdinal("GioiTinh"));
				if (!reader.IsDBNull(reader.GetOrdinal("HoKhau"))) entity.HoKhau = reader.GetString(reader.GetOrdinal("HoKhau"));
				if (!reader.IsDBNull(reader.GetOrdinal("CDUuTien"))) entity.CDUuTien = reader.GetString(reader.GetOrdinal("CDUuTien"));
				if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
			}
			reader.Close();
			return entity;
		}		
		
		//---------------------------------------------------------------------------------------------
		public static List<HocSinh> SelectCollectionAll()
		{
			List<HocSinh> collection = new List<HocSinh>();
			SqlDataReader reader = (SqlDataReader) SelectReaderAll();
			while (reader.Read())
			{
				HocSinh entity = new HocSinh();
				
				if (!reader.IsDBNull(reader.GetOrdinal("MaHSSV"))) entity.MaHSSV = reader.GetString(reader.GetOrdinal("MaHSSV"));
				if (!reader.IsDBNull(reader.GetOrdinal("TenHocSinh"))) entity.TenHocSinh = reader.GetString(reader.GetOrdinal("TenHocSinh"));
				if (!reader.IsDBNull(reader.GetOrdinal("NgaySinh"))) entity.NgaySinh = reader.GetDateTime(reader.GetOrdinal("NgaySinh"));
				if (!reader.IsDBNull(reader.GetOrdinal("DanToc"))) entity.DanToc = reader.GetString(reader.GetOrdinal("DanToc"));
				if (!reader.IsDBNull(reader.GetOrdinal("TonGiao"))) entity.TonGiao = reader.GetString(reader.GetOrdinal("TonGiao"));
				if (!reader.IsDBNull(reader.GetOrdinal("TPGD"))) entity.TPGD = reader.GetString(reader.GetOrdinal("TPGD"));
				if (!reader.IsDBNull(reader.GetOrdinal("GioiTinh"))) entity.GioiTinh = reader.GetBoolean(reader.GetOrdinal("GioiTinh"));
				if (!reader.IsDBNull(reader.GetOrdinal("HoKhau"))) entity.HoKhau = reader.GetString(reader.GetOrdinal("HoKhau"));
				if (!reader.IsDBNull(reader.GetOrdinal("CDUuTien"))) entity.CDUuTien = reader.GetString(reader.GetOrdinal("CDUuTien"));
				if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
				collection.Add(entity);
			}
			
			reader.Close();
			return collection;			
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public static List<HocSinh> SelectCollectionDynamic(string whereCondition, string orderByExpression)
		{
			List<HocSinh> collection = new List<HocSinh>();

			SqlDataReader reader = (SqlDataReader) SelectReaderDynamic(whereCondition, orderByExpression);
			while (reader.Read())
			{
				HocSinh entity = new HocSinh();
				
				if (!reader.IsDBNull(reader.GetOrdinal("MaHSSV"))) entity.MaHSSV = reader.GetString(reader.GetOrdinal("MaHSSV"));
				if (!reader.IsDBNull(reader.GetOrdinal("TenHocSinh"))) entity.TenHocSinh = reader.GetString(reader.GetOrdinal("TenHocSinh"));
				if (!reader.IsDBNull(reader.GetOrdinal("NgaySinh"))) entity.NgaySinh = reader.GetDateTime(reader.GetOrdinal("NgaySinh"));
				if (!reader.IsDBNull(reader.GetOrdinal("DanToc"))) entity.DanToc = reader.GetString(reader.GetOrdinal("DanToc"));
				if (!reader.IsDBNull(reader.GetOrdinal("TonGiao"))) entity.TonGiao = reader.GetString(reader.GetOrdinal("TonGiao"));
				if (!reader.IsDBNull(reader.GetOrdinal("TPGD"))) entity.TPGD = reader.GetString(reader.GetOrdinal("TPGD"));
				if (!reader.IsDBNull(reader.GetOrdinal("GioiTinh"))) entity.GioiTinh = reader.GetBoolean(reader.GetOrdinal("GioiTinh"));
				if (!reader.IsDBNull(reader.GetOrdinal("HoKhau"))) entity.HoKhau = reader.GetString(reader.GetOrdinal("HoKhau"));
				if (!reader.IsDBNull(reader.GetOrdinal("CDUuTien"))) entity.CDUuTien = reader.GetString(reader.GetOrdinal("CDUuTien"));
				if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
				collection.Add(entity);
			}
			
			reader.Close();
			return collection;			
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		

		public static DataSet SelectAll()
        {
            const string spName = "[dbo].[p_CDTM_HocSinh_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			
            return db.ExecuteDataSet(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_CDTM_HocSinh_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteDataSet(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
				
		public static IDataReader SelectReaderAll()
        {
            const string spName = "[dbo].[p_CDTM_HocSinh_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
            return db.ExecuteReader(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_CDTM_HocSinh_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteReader(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert methods.
		
		public static int InsertHocSinh(string tenHocSinh, DateTime ngaySinh, string danToc, string tonGiao, string TPGD, bool gioiTinh, string hoKhau, string cDUuTien, string ghiChu)
		{
			HocSinh entity = new HocSinh();	
			entity.TenHocSinh = tenHocSinh;
			entity.NgaySinh = ngaySinh;
			entity.DanToc = danToc;
			entity.TonGiao = tonGiao;
			entity.TPGD = TPGD;
			entity.GioiTinh = gioiTinh;
			entity.HoKhau = hoKhau;
			entity.CDUuTien = cDUuTien;
			entity.GhiChu = ghiChu;
			return entity.Insert();
		}
		
		public int Insert()
		{
			return this.Insert(null);
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public int Insert(SqlTransaction transaction)
		{			
			const string spName = "[dbo].[p_CDTM_HocSinh_Insert]";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@MaHSSV", SqlDbType.NVarChar, MaHSSV);
			db.AddInParameter(dbCommand, "@TenHocSinh", SqlDbType.NVarChar, TenHocSinh);
			db.AddInParameter(dbCommand, "@NgaySinh", SqlDbType.DateTime, NgaySinh.Year < 1753 ? DBNull.Value : (object) NgaySinh);
			db.AddInParameter(dbCommand, "@DanToc", SqlDbType.NVarChar, DanToc);
			db.AddInParameter(dbCommand, "@TonGiao", SqlDbType.NVarChar, TonGiao);
			db.AddInParameter(dbCommand, "@TPGD", SqlDbType.NVarChar, TPGD);
			db.AddInParameter(dbCommand, "@GioiTinh", SqlDbType.Bit, GioiTinh);
			db.AddInParameter(dbCommand, "@HoKhau", SqlDbType.NVarChar, HoKhau);
			db.AddInParameter(dbCommand, "@CDUuTien", SqlDbType.NVarChar, CDUuTien);
			db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, GhiChu);
			
			if (transaction != null)
			{
				return db.ExecuteNonQuery(dbCommand, transaction);
			}
            else
			{
				return db.ExecuteNonQuery(dbCommand);
			}			
		}
				
		//---------------------------------------------------------------------------------------------
		public static bool InsertCollection(IList<HocSinh> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using(SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (HocSinh item in collection)
						{
							if (item.Insert(transaction) <= 0)
							{							
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at InsertCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert / Update methods.
		
		public static int InsertUpdateHocSinh(string maHSSV, string tenHocSinh, DateTime ngaySinh, string danToc, string tonGiao, string TPGD, bool gioiTinh, string hoKhau, string cDUuTien, string ghiChu)
		{
			HocSinh entity = new HocSinh();			
			entity.MaHSSV = maHSSV;
			entity.TenHocSinh = tenHocSinh;
			entity.NgaySinh = ngaySinh;
			entity.DanToc = danToc;
			entity.TonGiao = tonGiao;
			entity.TPGD = TPGD;
			entity.GioiTinh = gioiTinh;
			entity.HoKhau = hoKhau;
			entity.CDUuTien = cDUuTien;
			entity.GhiChu = ghiChu;
			return entity.InsertUpdate();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int InsertUpdate()
		{
			return this.InsertUpdate(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int InsertUpdate(SqlTransaction transaction)
		{			
			const string spName = "p_CDTM_HocSinh_InsertUpdate";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@MaHSSV", SqlDbType.NVarChar, MaHSSV);
			db.AddInParameter(dbCommand, "@TenHocSinh", SqlDbType.NVarChar, TenHocSinh);
			db.AddInParameter(dbCommand, "@NgaySinh", SqlDbType.DateTime, NgaySinh.Year == 1753 ? DBNull.Value : (object) NgaySinh);
			db.AddInParameter(dbCommand, "@DanToc", SqlDbType.NVarChar, DanToc);
			db.AddInParameter(dbCommand, "@TonGiao", SqlDbType.NVarChar, TonGiao);
			db.AddInParameter(dbCommand, "@TPGD", SqlDbType.NVarChar, TPGD);
			db.AddInParameter(dbCommand, "@GioiTinh", SqlDbType.Bit, GioiTinh);
			db.AddInParameter(dbCommand, "@HoKhau", SqlDbType.NVarChar, HoKhau);
			db.AddInParameter(dbCommand, "@CDUuTien", SqlDbType.NVarChar, CDUuTien);
			db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, GhiChu);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);			
		}
		
		//---------------------------------------------------------------------------------------------
		public static bool InsertUpdateCollection(IList<HocSinh> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (HocSinh item in collection)
						{
							if (item.InsertUpdate(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at InsertUpdateCollection method: " + ex.Message);
						
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}	
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Update methods.
		
		public static int UpdateHocSinh(string maHSSV, string tenHocSinh, DateTime ngaySinh, string danToc, string tonGiao, string TPGD, bool gioiTinh, string hoKhau, string cDUuTien, string ghiChu)
		{
			HocSinh entity = new HocSinh();			
			entity.MaHSSV = maHSSV;
			entity.TenHocSinh = tenHocSinh;
			entity.NgaySinh = ngaySinh;
			entity.DanToc = danToc;
			entity.TonGiao = tonGiao;
			entity.TPGD = TPGD;
			entity.GioiTinh = gioiTinh;
			entity.HoKhau = hoKhau;
			entity.CDUuTien = cDUuTien;
			entity.GhiChu = ghiChu;
			return entity.Update();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int Update()
		{
			return this.Update(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Update(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_CDTM_HocSinh_Update]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@MaHSSV", SqlDbType.NVarChar, MaHSSV);
			db.AddInParameter(dbCommand, "@TenHocSinh", SqlDbType.NVarChar, TenHocSinh);
			db.AddInParameter(dbCommand, "@NgaySinh", SqlDbType.DateTime, NgaySinh.Year == 1753 ? DBNull.Value : (object) NgaySinh);
			db.AddInParameter(dbCommand, "@DanToc", SqlDbType.NVarChar, DanToc);
			db.AddInParameter(dbCommand, "@TonGiao", SqlDbType.NVarChar, TonGiao);
			db.AddInParameter(dbCommand, "@TPGD", SqlDbType.NVarChar, TPGD);
			db.AddInParameter(dbCommand, "@GioiTinh", SqlDbType.Bit, GioiTinh);
			db.AddInParameter(dbCommand, "@HoKhau", SqlDbType.NVarChar, HoKhau);
			db.AddInParameter(dbCommand, "@CDUuTien", SqlDbType.NVarChar, CDUuTien);
			db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, GhiChu);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
				
		//---------------------------------------------------------------------------------------------
		public static bool UpdateCollection(IList<HocSinh> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (HocSinh item in collection)
						{
							if (item.Update(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at UpdateCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Delete methods.
		
		public static int DeleteHocSinh(string maHSSV)
		{
			HocSinh entity = new HocSinh();
			entity.MaHSSV = maHSSV;
			
			return entity.Delete();
		}
		
		public int Delete()
		{
			return this.Delete(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Delete(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_CDTM_HocSinh_Delete]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@MaHSSV", SqlDbType.NVarChar, MaHSSV);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
		
		public static bool DeleteCollection(IList<HocSinh> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (HocSinh item in collection)
						{
							if (item.Delete(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at DeleteCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		#endregion
	}	
}